package com.mljr.example.util;

import android.content.SharedPreferences;

import com.ctakit.sdk.SdkApplication;

public class SharedPreferenceUtil {


    private static SharedPreferences preferences;

    private static String sharedPreferences = "config";

    private static synchronized SharedPreferences getSharedPreferences() {
        if (preferences == null) {
            preferences = SdkApplication.app().getSharedPreferences("config", 0);
        }
        return preferences;
    }

    public static void putStringValueByKey(String key,
                                           String value) {
        try {
            getSharedPreferences().edit().putString(key, value).commit();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getStringFromSharedPreference(
            String key, String defaultValue) {
        SharedPreferences shared = getSharedPreferences();
        if (shared != null) {
            return shared.getString(key, defaultValue);
        }
        return defaultValue;
    }

    public static void putLongValueByKey(String key, long value) {
        try {
            getSharedPreferences().edit().putLong(key, value).commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static long getLongFromSharedPreference(String key,
                                                   long defaultValue) {
        SharedPreferences shared = getSharedPreferences();
        if (shared != null) {
            return shared.getLong(key, defaultValue);
        }
        return defaultValue;
    }

    public static void putIntValueByKey(String key, int value) {
        getSharedPreferences().edit().putInt(key, value).commit();
    }

    public static int getIntValueByKey(String key,
                                       int defaultValue) {
        SharedPreferences shared = getSharedPreferences();
        if (shared != null) {
            return shared.getInt(key, defaultValue);
        }
        return defaultValue;
    }

    public static void putFloatValueByKey(String key,
                                          float value) {
        getSharedPreferences().edit().putFloat(key, value).commit();
    }

    public static float getFloatValueByKey(String key,
                                           int defaultValue) {
        SharedPreferences shared = getSharedPreferences();
        if (shared != null) {
            return shared.getFloat(key, defaultValue);
        }
        return defaultValue;
    }

    public static void putBooleanByKey(String key,
                                       boolean value) {
        getSharedPreferences().edit().putBoolean(key, value).commit();
    }

    public static boolean getBooleanByKey(String key,
                                          boolean defaultValue) {
        SharedPreferences shared = getSharedPreferences();
        if (shared != null) {
            return shared.getBoolean(key, defaultValue);
        }
        return defaultValue;
    }
}